#include "parser/system_file_parser.h"

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <ctime>

#include "absl/container/flat_hash_map.h"
#include <glog/logging.h>

namespace sysinfo {
namespace monitor {

// 注意：构造函数已经在头文件中默认定义，不要在这里重复定义

bool SystemFileParser::Parse(const std::string& command_name, 
                            const std::string& output,
                            SystemInfoProto* system_info_proto) {
    if (!system_info_proto) {
        return false;
    }

    // 根据命令名称选择相应的解析方法
    if (command_name == "meminfo") {
        return ParseProcMeminfo(output, system_info_proto);
    } else if (command_name == "cpuinfo") {
        return ParseProcCpuinfo(output, system_info_proto);
    } else if (command_name == "loadavg") {
        return ParseProcLoadavg(output, system_info_proto);
    } else if (command_name == "stat") {
        return ParseProcStat(output, system_info_proto);
    } else if (command_name == "vmstat") {
        return ParseProcVmstat(output, system_info_proto);
    } else if (command_name == "diskstats") {
        return ParseProcDiskstats(output, system_info_proto);
    } else if (command_name == "netdev") {
        return ParseProcNetDev(output, system_info_proto);
    }

    // 未知命令
    LOG(ERROR) << "Unknown system file to parse: " << command_name;
    return false;
}

bool SystemFileParser::ParseProcMeminfo(const std::string& output, SystemInfoProto* system_info_proto) {
    // 实现/proc/meminfo文件内容解析
    SystemFileResult* result = system_info_proto->add_file_results();
    result->set_name("meminfo");
    
    // 设置时间戳
    time_t now = std::time(nullptr);
    result->set_timestamp(std::to_string(now));
    result->set_content(output);
    
    return true;
}

bool SystemFileParser::ParseProcCpuinfo(const std::string& output, SystemInfoProto* system_info_proto) {
    // 实现/proc/cpuinfo文件内容解析
    SystemFileResult* result = system_info_proto->add_file_results();
    result->set_name("cpuinfo");
    
    // 设置时间戳
    time_t now = std::time(nullptr);
    result->set_timestamp(std::to_string(now));
    result->set_content(output);
    
    return true;
}

bool SystemFileParser::ParseProcLoadavg(const std::string& output, SystemInfoProto* system_info_proto) {
    // 实现/proc/loadavg文件内容解析
    SystemFileResult* result = system_info_proto->add_file_results();
    result->set_name("loadavg");
    
    // 设置时间戳
    time_t now = std::time(nullptr);
    result->set_timestamp(std::to_string(now));
    result->set_content(output);
    
    return true;
}

bool SystemFileParser::ParseProcStat(const std::string& output, SystemInfoProto* system_info_proto) {
    // 实现/proc/stat文件内容解析
    SystemFileResult* result = system_info_proto->add_file_results();
    result->set_name("stat");
    
    // 设置时间戳
    time_t now = std::time(nullptr);
    result->set_timestamp(std::to_string(now));
    result->set_content(output);
    
    return true;
}

bool SystemFileParser::ParseProcVmstat(const std::string& output, SystemInfoProto* system_info_proto) {
    // 实现/proc/vmstat文件内容解析
    SystemFileResult* result = system_info_proto->add_file_results();
    result->set_name("vmstat");
    
    // 设置时间戳
    time_t now = std::time(nullptr);
    result->set_timestamp(std::to_string(now));
    result->set_content(output);
    
    return true;
}

bool SystemFileParser::ParseProcDiskstats(const std::string& output, SystemInfoProto* system_info_proto) {
    // 实现/proc/diskstats文件内容解析
    SystemFileResult* result = system_info_proto->add_file_results();
    result->set_name("diskstats");
    
    // 设置时间戳
    time_t now = std::time(nullptr);
    result->set_timestamp(std::to_string(now));
    result->set_content(output);
    
    return true;
}

bool SystemFileParser::ParseProcNetDev(const std::string& output, SystemInfoProto* system_info_proto) {
    // 实现/proc/net/dev文件内容解析
    SystemFileResult* result = system_info_proto->add_file_results();
    result->set_name("netdev");
    
    // 设置时间戳
    time_t now = std::time(nullptr);
    result->set_timestamp(std::to_string(now));
    result->set_content(output);
    
    return true;
}

}  // namespace monitor
}  // namespace qcraft